# coding:utf-8
# account.py

import os
import re
class Loadaccount:

	def __init__(self,file):
		_=r'^#.*'
		str_macaddress=r'(([0-9a-f]{2}:){5}[0-9a-f]{2})?'
		str_default=r'^\s*default\s*=\s*(?P<default_account>\w*)'
		str_account=r'^\s*(?P<account_name>\w*)\s*::\s*(?P<username>\w*)\s*::\s*(?P<password>\w*)\s*::\s*(?P<mac_address>'+str_macaddress+')?\s*$'
		self.pattern_default=re.compile(str_default,re.I|re.X)
		self.pattern_account=re.compile(str_account,re.I|re.X)
		self.pattern_blank=re.compile(r'\n\s*\r')
		self._=re.compile(_)
		self.__accounts=[]
		self.__defautlAccountName=""
		try:
			self.__accounts=self.get_account_from_file(file)
		except 'error',data:
			print "at line: " + str(data[1])
			print str(data[0])
	
	def get_default_account_name(self):
		return self.__defautlAccountName
	
	def get_accounts(self):
		return self.__accounts
	
	def get_account_from_file(self,file):
		if not os.path.isfile(file): raise 'No such file:' + file
		ret = []
		afile=open(file)
		line_num=0
		line=afile.readline()
		while(line):
			line_num+=1
			if(self.pattern_account.match(line)):
				match_list=self.pattern_account.match(line)
				_account=Account(match_list.group("account_name"),match_list.group("username"),
								 match_list.group("password"),match_list.group("mac_address"))
				ret.append(_account)
			elif(self.pattern_default.match(line)):
				match_list=self.pattern_default.match(line)
				self.__defautlAccountName=match_list.group("default_account")
			elif(self._.match(line)):
				None
			elif(self.pattern_blank.match(line)):
				raise 'error',[line,line_num]
			line=afile.readline()
		return ret

	def get_account_by_name(self,accountname):
		ret = Account()
		if self.__accounts == []:
			return ret
		for account in self.__accounts:
			if(accountname == account.get_account_name()):
				return account
			continue
		return ret

class Account():
	def __init__(self,accountName="",username="",
				 password="0000",macaddress=""):
		self.__username=username
		self.__password=password
		self.__macAddress=macaddress
		self.__accountName=accountName

	def printa(self):
		print "account_name: " + self.__accountName
		print "username: " + self.__username
		print "password: " + self.__password
		print "macaddress: " + self.__macAddress
		
	def get_account_name(self):
		return self.__accountName
	def set_account_name(self,accountName):
		self.__accountName=accountName

	def setMac(self,macAddress):
		self.__macAddress=macAddress
	def getMac(self):
		return self.__macAddress

	def set_account(self,username,password="0000"):
		self.__username=username
		self.__password=password

	def set_username(self,username):
		self.__username=username
	def get_username(self):
		return self.__username

	def set_password(self,password):
		self.__password=password
	def get_password(self):
		return self.__password

	def isValid(self):
		return (self.__username != "")

loadaccount=Loadaccount("account.conf")
default_account=loadaccount.get_account_by_name(loadaccount.get_default_account_name())
if __name__ == "__main__":
        default_account.printa()
